<template>
	<view class="container">
		<view class="top_btn">
			<view class="password_btn">
				<u-button shape="circle" color="var(--echo-main-color)" @click="submit" size="small">确定
				</u-button>
			</view>
		</view>
		<u-upload class="form_upload" :previewFullImage="true" @afterRead="afterRead" @delete="deletePic" name="1"
			multiple :maxCount="1" :fileList="fileList1">
			<view class="portrait">
				<view class="portrait_box">
					<image :src="portraitUrl" mode=""></image>
				</view>
				<view class="portrait_do">
					<u-icon name="camera-fill" color="#fe7c24" size="20"></u-icon>
				</view>
			</view>
		</u-upload>
	</view>
</template>

<script>
	import {
		settingCostomerInfo,
		getCustomerDetail
	} from '@/config/customer.js';
	export default {
		data() {
			return {
				ipfsUrl: '',
				customerInfo: '',
				fileList1: [],
				portraitUrl: ''
			}
		},
		onShow() {
			this.customerInfo = uni.getStorageSync('customerInfo');
			this.portraitUrl = this.customerInfo.avatar
		},
		created() {
			this.ipfsUrl = getApp().globalData.ipfsUrl
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let ipfsUploadUrl = this.ipfsUrl
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: ipfsUploadUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res)
							var resJon = JSON.parse(res.data)
							setTimeout(() => {
								resolve(resJon.data.fileUrl)
							}, 1000)
						}
					});
				})
			},

			async submit() {
				console.log(this.fileList1[0].url)
				// 请求后台修改头像

				let res = await settingCostomerInfo({
					avatar: this.fileList1[0].url,
				})

				getCustomerDetail({}).then(res => {
					if (res)
						uni.setStorageSync('customerInfo', res.data.customerInfo);
					uni.reLaunch({
						url: '/pages/My/My'
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: #F3F4F8;

		.top_btn {
			display: flex;
			justify-content: flex-end;

			.password_btn {
				margin: 30rpx;
				width: 160rpx;
			}
		}

		::v-deep .u-upload__wrap {
			display: flex;
			align-items: center;
			justify-content: center;
		}

		::v-deep .u-upload__wrap__preview__image {
			width: 200rpx !important;
			height: 200rpx !important;
			border-radius: 50%;
			overflow: hidden;
		}

		.portrait {
			position: relative;

			&_box {
				width: 200rpx;
				height: 200rpx;
				border-radius: 50%;
				overflow: hidden;
				border: 2rpx solid #ddd;

				image {
					width: 100%;
					height: 100%;
				}
			}

			&_do {
				position: absolute;
				bottom: 8rpx;
				right: 8rpx;
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				background-color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				border: 1px solid #ddd;
			}
		}
	}
</style>
